
(function($) {
  $.fn.eteCheckList = function(options) {
	/* mi serve il trim, lo aggiungo se non esistesse */
	if (!String.prototype.trim){String.prototype.trim=function(){return this.replace(/^\s+/,'').replace(/\s+$/,'');}}
	var opts = $.extend({}, $.fn.eteCheckList.defaults, options);
	return this.each(function() {
		var $this = $(this);
		var div = this;
		var o = $.metadata ? $.extend({}, opts, $this.metadata()) : opts;
		if (o.onText){
                        if (o.textId){
                                this.chkText = jQuery("#"+o.textId)[0];
                        }else{
                                this.chkText = jQuery('<input type="text" class="eteCheckList_checkText" value="" />')[0];
                                $this.after(this.chkText);
                        }
                        this.chkText.genitore = this;
                        $(this.chkText).blur(function(){this.genitore.eteRiToggle();});
                        if(o.singleClick){
                                $(this.chkText).click(function(){$(this.genitore).toggle();})
                        }else{
                                $(this.chkText).dblclick(function(){$(this.genitore).show();})
                                $(this.chkText).click(function(){$(this.genitore).hide();})
                        }
                }
		this.eteToggle = function (){
			this.chkText.value = '';
			this.iCheckBox.each(function(){if(this.checked)this.genitore.chkText.value += this.label + ", ";});
			if (this.chkText.value.substring(-2)==', ') this.chkText.value = this.chkText.value.substring(0,this.chkText.value.length-2);
		}
		this.eteRiToggle = function(){
			var labels = this.chkText.value.split(',');
			this.tmpStr = '';
			this.iCheckBox.each(function(){
				this.checked = false;
				for (var i = 0 ; i < labels.length; i++){
					if(this.checked |= ((this.label.toLowerCase() == labels[i].trim().toLowerCase())||(this.value == labels[i].trim()))) {
						this.genitore.tmpStr+=this.label+", ";
						break;
					}
				}
			});
			this.chkText.value = this.tmpStr;
			this.tmpStr = null;
			if (this.chkText.value.substring(-2)==', ') this.chkText.value = this.chkText.value.substring(0,this.chkText.value.length-2);
		}
		this.iCheckBox = jQuery('input.eteCheckList_check',$this);
		this.iCheckBox.each(function(){
			var o = $.metadata ? $.extend({}, opts, $(this).metadata()) : opts;
			this.value = o.value;
			this.name = o.name;
			this.genitore = div;
			this.id = 'etechk_' + new Date().valueOf();
			var label = jQuery('<label for="' + this.id + '">'+ o.label + '</label>');
			this.label = o.label;
			$(this).wrap('<div class="eteCheckList_checkDiv"/>');
			$(this).click(function(){this.genitore.eteToggle();});
			$(this).after(label);
		})
                if(o.hideBox) $this.hide();
	}); 
  };
  function debug($obj) {
	if (window.console && window.console.log)
	  window.console.debug($obj);
  };
  $.fn.eteCheckList.defaults = {
	onText		: true,		/* when check a box the checkbox label go in the textBox */
	textId		: null,		/* the id for the textbox/textarea who show the checked  if onText and this is null it will be created */
	hideBox		: true,		/* hides the div with checkboxs when lose focus */
	singleClick	: false		/* if true use single click for show the checkBoxes else use dblclick */
        
  };
})(jQuery);

